package com.hqyj.controller;

import com.hqyj.dao.AchievementDao;
import com.hqyj.dao.InfoDao;
import com.hqyj.entity.Achievement;
import com.hqyj.entity.Check;
import com.hqyj.entity.Info;
import com.hqyj.entity.Info1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ac")
public class AchievementController {
    @Autowired
    AchievementDao achievementDao;

    @Autowired
    InfoDao infoDao;

    @RequestMapping("/allac/{id}")
    String allac(@PathVariable Integer id, Model m/*Integer pageNum*/){
/*        System.out.println("!!!!!!!!!!!!!!!!!"+pageNum);
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum-1,2);*/
        System.out.println(id+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        List<Achievement> ac = achievementDao.QueryACById(id);
        String name = achievementDao.getname(id);
        System.out.println(ac);
        m.addAttribute("achievement",ac);
        m.addAttribute("name",name);
        return "admin/achievement";
    }

    @RequestMapping("/gz/{id}")
    @ResponseBody
   String money(@PathVariable Integer id){
        System.out.println("id+#######################################################################");
        String name = achievementDao.getname(id);
        Calendar c = Calendar.getInstance();
        Integer year=c.get(Calendar.YEAR);
        Integer mounth=c.get(Calendar.MONTH)+1;
        System.out.println(year+mounth+"########################################################################");
        double x = achievementDao.FindCheak(id,mounth,year);
        double y = achievementDao.GetMoney(id,mounth,year);
        double money = 3000+(y*0.02*4000)-(100*x);
        return name+year+"年"+mounth+"月工资为"+money+"元";
    }
    @RequestMapping("/checkall")
    String checklist(Model m){
        List<Map<String,Check>>  checks= achievementDao.QueryCheck();
        m.addAttribute("checks",checks);
        System.out.println(checks+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        return "admin/checklist";
    }
    @RequestMapping("/goadd")
    String goadd(){
        return "admin/addcheck";
    }


    @RequestMapping("/addcheck")
    String add(Integer user_id,String checkstate,String reason,String checkyear,String checkmounth,String checkday){
        System.out.println(user_id+checkday+checkmounth+checkstate+checkyear+reason);
        int num = achievementDao.insertCheck(user_id,checkstate,reason,checkyear,checkmounth,checkday);
        if (num>0) {
            return "redirect:checkall";
        }else {
            System.out.println("插入失败！！！！！！！！！！！！！！！！");
            return "redirect:checkall";
        }
    }

    @RequestMapping("/goinfo")
    String goinfo(Model m,Integer pageNum){
        System.out.println("12122121"+pageNum);
        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum-1,4);
        Page<Map<String, Info1>> infos= achievementDao.getInfo(pageable);
        m.addAttribute("infos",infos);
        System.out.println(infos);
        System.out.println(pageable+"+++++++++++++++++++++++++");
        return "admin/infolist";
    }

    @RequestMapping("/onread/{info_id}")
    String onread(@PathVariable Integer info_id,Model m){
        System.out.println(info_id+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        int num= achievementDao.inforeay(info_id);
        System.out.println(num+"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        return "redirect:../goinfo";
    }




}
